package b_workqueue;

import com.rabbitmq.client.*;
import org.junit.Test;
import utils.RabbitMQUtils;

import java.io.IOException;

public class Consumer2 {
    @Test
    public void receiveMessage() throws Exception {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        // channel.basicQos(1);//一次只处理一条消息
        channel.queueDeclare("hello", false, false, false, null);
        //把签收模式变成 false
        channel.basicConsume("hello", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        // try {
        // Thread.sleep(500);
        // } catch (InterruptedException e) {
        // e.printStackTrace();
        // }
        //手动签收
        //channel.basicAck(envelope.getDeliveryTag(),false);
                System.out.println("消费者【2】收到消息：" + new String(body));
            }
        });
        System.out.println("消费者【2】启动成功");
        //不能让程序结束
        System.in.read();
        //关闭
        RabbitMQUtils.closeChannelAndConnection(channel, connection);
    }
}